package com.ocom.mqsimple.business.human;

import com.alibaba.fastjson.JSON;
import com.ocom.common.response.web.ExcelApplyRequest;
import com.ocom.common.vo.PersonVo;
import com.ocom.mqsimple.config.HumanMqConfig;
import com.ocom.mqsimple.feign.HumanClientService;
import com.ocom.mqsimple.feign.WebApiClientService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.Map;


@Component
public class HumanConsumerMqListener {

	private Logger logger = LoggerFactory.getLogger(HumanConsumerMqListener.class);


	@Autowired
	HumanClientService humanClientService;

	/**
	 * 把该监听命名为A监听
	 * 监听队列  远程服务支付接口发送消息  该队列会接受
	 * [message, headers, channel]
	 * @return {@link }
	 * @throws
	 */
	@RabbitListener(queues = HumanMqConfig.HUMAN_QUEUE)
	public void process(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
		logger.info("human_queue 消费中");
		logger.info("message【{}】",message);
		String humanMessage = new String(message.getBody(), StandardCharsets.UTF_8);
		//TODO 数据库操作样例
		logger.info("humanMessage-start:【{}】",humanMessage);
		logger.info("System.currentTimeMillis()【{}】",System.currentTimeMillis());


		try {

			PersonVo personVo= JSON.parseObject(humanMessage, PersonVo.class);
			humanClientService.UpdatePerson(personVo);

		} catch (Exception e) {
			logger.error("消息处理失败:{{}}",humanMessage);
			logger.info("出现异常队列消息开始进入死信队列{{}}",e.getMessage());
		}


		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		logger.info("humanMessage-end:【{}】",humanMessage);
		logger.info("System.currentTimeMillis()【{}】",System.currentTimeMillis());
	}

	/**
	 * 把该监听命名为B监听
	 * 监听死信队列  远程服务支付接口发送消息  该队列会接受
	 * [message, headers, channel]
	 * @return {@link }
	 * @throws
	 */
	@RabbitListener(queues =  HumanMqConfig.HUMAN_DIE_QUEUE)
	public void processForDie(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
		//死信队列放弃
		channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);

	}

}
